package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.AdvertisingPlace;
import com.example.buildtable.service.AdvertisingPlaceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "广告位")
@RequestMapping(value = "/advertisingPlace/")
public class AdvertisingPlaceController {
    @Autowired
    private AdvertisingPlaceService advertisingPlaceService;
    
    @Operation(summary = "查询所有广告位数据")
    @GetMapping(value = "/getAllAdvertisingPlace")
    public R getAllAdvertisingPlace() {
        List<AdvertisingPlace> advertisingPlaceList = advertisingPlaceService.list();
        return R.ok("查询所有广告位数据成功").data("advertisingPlaceList", advertisingPlaceList);
    }
    
    @Operation(summary = "增加广告位")
    @PostMapping(value = "/addAdvertisingPlace")
    public R addAdvertisingPlace(AdvertisingPlace advertisingPlace) {
        boolean flag = advertisingPlaceService.save(advertisingPlace);
        if (flag) {
            return R.ok("增加广告位成功");
        } else {
            return R.ok("增加广告位失败");
        }
    }

    @Operation(summary = "删除广告位")
    @GetMapping(value = "/deleteAdvertisingPlace/{advertisingPlaceId}")
    public R deleteAdvertisingPlace(@PathVariable("advertisingPlaceId") Integer advertisingPlaceId) {
        boolean flag = advertisingPlaceService.removeById(advertisingPlaceId);
        if (flag) {
            return R.ok("删除广告位成功");
        } else {
            return R.ok("删除广告位失败");
        }
    }

    @Operation(summary = "按照广告位查看")
    @GetMapping(value = "/getAdvertisingPlaceById/{advertisingPlaceId}")
    public R getAdvertisingPlaceById(@PathVariable("advertisingPlaceId") Integer advertisingPlaceId) {
        AdvertisingPlace advertisingPlace = advertisingPlaceService.getById(advertisingPlaceId);
        if (advertisingPlace != null) {
            return R.ok("按照广告位查看成功").data("advertisingPlace", advertisingPlace);
        } else {
            return R.error("按照广告位查看失败");
        }
    }

    @Operation(summary = "修改广告位")
    @PostMapping(value = "/updateAdvertisingPlace")
    public R updateAdvertisingPlace(AdvertisingPlace advertisingPlace) {
        boolean flag = advertisingPlaceService.updateById(advertisingPlace);
        if (flag) {
            return R.ok("修改广告位成功");
        } else {
            return R.ok("修改广告位失败");
        }
    }
}